using DynamicModelDemo.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Auditing;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace DynamicModelDemo.EntityFramework
{
    public class MyDbContext : DbContext, IHasCreationTime
    {
        public virtual DbSet<StudentEntity> Students => base.Set<StudentEntity>();

        public DateTime CreationTime { get; set; }

        public MyDbContext()
        {
            CreationTime = DateTime.Now;
        }
        public MyDbContext(DateTime time)
        {
            CreationTime = time;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=DynamicModels;Integrated Security=true");
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StudentEntity>(b =>
            {
                b.ToTable($"students-{CreationTime.ToString("yyyy-MM-dd")}");
                b.HasKey(x => x.Id);
                b.ConfigureByConvention();
                b.ConfigureExtraProperties();
            });
            base.OnModelCreating(modelBuilder);
        }
    }
}
